package com.cssw.fyzb.web.service;

import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.core.dao.live.AnchorDao;
import com.cssw.fyzb.core.dao.live.LiveDao;
import com.cssw.fyzb.core.dao.live.LiveReportDao;
import com.cssw.fyzb.core.dao.user.UserDao;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.web.vo.LiveReportVo;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class LiveReportService {
    @Autowired
    private LiveReportDao liveReportDao;

    @Autowired
    private LiveDao liveDao;

    @Autowired
    private UserDao userDao;
    @Autowired
    private AnchorDao anchorDao;
    /**
     * 查询菜单列表
     *
     * @param page
     * @param limit
     * @param param
     * @return
     */
    public AjaxPage list(int page, int limit, Map param) {

        Page<CmfLiveReport> pages = liveReportDao.list(page, limit, param);
        List<LiveReportVo> liveReportVos = new ArrayList<>();
        if (null != pages.getList() && pages.getList().size() != 0) {
            LiveReportVo LiveReVo = null;
            for (CmfLiveReport live: pages.getList()) {
                CmfLive cmfLive  = liveDao.findOne(live.getLiveId());
                User    userFrom = userDao.getById(live.getFromUserId());
                User    userTo   = userDao.getById(live.getToUserId());
                LiveReVo = new LiveReportVo();
                LiveReVo.setReportId(live.getReportId());
                LiveReVo.setLiveId(live.getLiveId());
                LiveReVo.setFromUserId(live.getFromUserId());
                LiveReVo.setToUserId(live.getToUserId());
                LiveReVo.setContent(live.getContent());
                LiveReVo.setStatus(live.getStatus());
                LiveReVo.setCreateTime(live.getCreateTime());
                LiveReVo.setFromUserName(userFrom.getUserName());
                LiveReVo.setToUserName(userTo.getUserName());
                LiveReVo.setToUserName(userTo.getUserName());
                LiveReVo.setPull(cmfLive.getPull());
                LiveReVo.setThumb(cmfLive.getThumb());
                LiveReVo.setLStatus(cmfLive.getStatus());
                liveReportVos.add(LiveReVo);
            }
        }
        return AjaxPage.success(liveReportVos, pages.getTotalRow(), pages.getTotalPage());
    }


    public boolean update(Long reportId){
      Boolean flag = Db.tx(() -> {
          CmfLiveReport cmfLiveReport= liveReportDao.findOne(reportId);
          CmfAnchor cmfAnchor=anchorDao.queryAnchorByUserId(cmfLiveReport.getToUserId());
          anchorDao.queryAnchorSaveSate(cmfAnchor.getAnchorId(),cmfLiveReport.getToUserId(),"2");
          liveReportDao.save(reportId,"2");
          return true;
        });
        return flag;
    }

    public boolean reject(Long reportId){
        Boolean flag = Db.tx(() -> {
            CmfLiveReport cmfLiveReport= liveReportDao.findOne(reportId);
            CmfAnchor cmfAnchor=anchorDao.queryAnchorByUserId(cmfLiveReport.getToUserId());
            anchorDao.queryAnchorSaveSate(cmfAnchor.getAnchorId(),cmfLiveReport.getToUserId(),"2");
            liveReportDao.save(reportId,"3");
            return true;
        });
        return flag;
    }
}
